<template>
  <div>
    <div class="container">
      <div class="search-box">
        <el-row :gutter="10">
          <el-col :span="16" class="se">
            <el-button @click="gotoTargetPage" class="back">返回</el-button>
          </el-col>
          <div class="acatar">
            <UserAcatar @transfer="change" />
          </div>
        </el-row>
      </div>
    </div>
    <div class="work-detail">
      <div class="score-title">
        <span class="score-number">{{ work.score }}</span>
        <span class="score-total">/100</span>
      </div>
      <div class="container"></div>
    </div>
    <div class="work-info">
      <div class="container">
        <h1 class="work-title">{{ work.name }}</h1>
        <div class="work-desc">
          <p class="student-name">学生：{{ work.student }}</p>
          <p class="submit-time">提交时间：{{ work.submitTime }}</p>
        </div>
      </div>
    </div>

    <div class="work-detail-page">
      <div class="container">
        <div class="work-content">
          <div class="work-content-desc">
            <el-card>
              <div v-for="(question, index) in questions" :key="index">
                <el-divider>{{ index + 1 }}、{{ question.text }}</el-divider>
                <div v-if="question.type === 'single-choice'">
                  <el-radio-group v-model="question.userAnswer">
                    <el-radio
                      v-for="(choice, choiceIndex) in question.choices"
                      :key="choiceIndex"
                      :label="choice"
                      >{{ choice }}</el-radio
                    >
                  </el-radio-group>
                </div>
                <div v-else-if="question.type === 'short-answer'">
                  <el-input
                    v-model="question.userAnswer"
                    type="textarea"
                    :autosize="{ minRows: 2, maxRows: 4 }"
                    placeholder="请输入答案"
                  ></el-input>
                </div>
              </div>
              <div style="text-align: center">
              </div>
            </el-card>
          </div>
        </div>
        <div class="work-score">
          <div class="score-comment">
            <h3 class="score-comment-title">评语</h3>
            <div class="mojitou">
              <p class="score-comment-content">{{ work.comment }}</p>
            </div>
          </div>
          <div class="operate-buttons">
            <el-button
              @click="editScore"
              style="background-color: #fdcb9e; color: white"
              >编辑得分</el-button
            >
          </div>
        </div>
      </div>
      <el-dialog title="编辑得分" :visible.sync="dialogVisible" width="50%">
        <div class="score-dialog">
          <el-form ref="form" :model="correctForm" label-width="80px">
            <el-form-item label="成绩">
              <el-input
                v-model.number="correctForm.score"
                placeholder="请打分"
              ></el-input>
            </el-form-item>
            <el-form-item label="评语">
              <el-input
                v-model="correctForm.comment"
                placeholder="留下您的评语"
              ></el-input>
            </el-form-item>
          </el-form>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitCorrect">提交</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import UserAcatar from "@/components/UserAcatar.vue";
export default {
  name: "corrent",
  components: {
    UserAcatar,
  },
  data() {
    return {
      work: {
        id: 1,
        name: "第一次作业",
        student: "张三",
        submitTime: "2021-07-01 12:00:00",
        endTime: "2021-07-10 23:59:59",
        status: "已批改",
        score: "",
        comment: "",
        content:
          "请你们阅读英文网页的“Web Scraping with Beautiful Soup”，然后根据网页中提供的指导抓取Amazon.com上的所有书籍信息。抓取的信息包括：书名、作者、评分、价格、库存情况和商品链接。抓取结果存储为一个CSV文件。",
      },
      dialogVisible: false,
      correctForm: {
        score: 0,
        comment: "",
      },
      questions: [
        {
          type: "single-choice",
          text: "以下哪个是 JavaScript 的基本数据类型？",
          choices: ["Number", "String", "Boolean", "All of the above"],
          answer: 3,
          userAnswer: null,
        },
        {
          type: "short-answer",
          text: "请简述 Vue.js 的生命周期。",
          answer: "Vue.js 的生命周期包括创建、挂载、更新、销毁四个阶段。",
          userAnswer: "",
        },
      ],
    };
  },
  mounted() {},
  methods: {
    gotoTargetPage() {
      this.$router.push({ path: "tcorrent" });
    },
    change(msg) {
      this.$emit("transfer", msg);
    },
    editScore() {
      this.dialogVisible = true;
      this.correctForm.score = this.work.score;
      this.correctForm.comment = this.work.comment;
    },
    submitCorrect() {
      if (
        !/^\d+$/.test(this.correctForm.score) ||
        this.correctForm.score < 0 ||
        this.correctForm.score > 100
      ) {
        this.$message.error("请输入零到一百的整数");
        return;
      }
      // 提交批改结果
      console.log(this.correctForm);
      // 更新成绩和评语
      this.work.score = this.correctForm.score;
      this.work.comment = this.correctForm.comment;
      // 关闭弹窗
      this.dialogVisible = false;
      // 清空表单
      this.$refs.form.resetFields();
    },
  },
  computed: {
    currentData() {
      const startIndex = (this.currentPage - 1) * this.pageSize;
      const endIndex = startIndex + this.pageSize;
      return this.students.slice(startIndex, endIndex);
    },
  },
};
</script>
<style scoped>
.el-icon-search {
  color: #cceabb;
}

.search-box {
  border-bottom: 2px solid #bbb;
  height: 80px;
}
.se {
  margin: 0 auto;
}

.acatar {
  float: right;
}
.search {
  color: #fdcb9e;
  font-size: 18px;
  width: 300px;
  margin-left: 100px;
  margin-top: 20px;
}
.mojitou {
  width: 500px;
  height: 120px;
  border: 1px solid #bbb;
  margin: 0 auto;
  border-radius: 5%;
}
.back {
  margin-top: 20px;
}
.work-detail-page {
  padding: 20px;
}
.operate-buttons {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
.work-detail-page {
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.work-info {
  text-align: center;
  margin-bottom: 50px;
}
.work-status {
  font-size: 20px;
  color: #67c23a;
}
.work-score {
  text-align: center;
  margin-bottom: 50px;
}
.score-title {
  display: flex;
  align-items: baseline;
  font-size: 60px;
  font-weight: bold;
  color: #409eff;
}

.score-comment {
  margin-top: 20px;
  text-align: left;
}
.operate-buttons {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
.score-dialog {
  padding: 20px;
}
.container {
  max-width: 100%;
  margin: 0 auto;
}

.container .el-input__inner {
  font-size: 16px;
}

.avatar {
  float: right;
  margin-top: 15px;
}

.work-detail-page {
  padding: 20px;
}

.work-info {
  background-color: #fff;
  margin-bottom: 20px;
}

.work-title {
  font-size: 30px;
  text-align: center;
  padding-top: 50px;
}

.work-desc {
  width: 600px;
  margin: 0 auto;
  padding-bottom: 20px;
}

.student-name,
.submit-time,
.work-status {
  display: inline-block;
  font-size: 18px;
  margin-right: 40px;
  color: #666;
}

.work-content-title {
  margin: 0 auto;
  font-size: 24px;
  font-weight: bold;
  color: #444;
}

.work-content-desc {
  margin-top: 20px;
  font-size: 16px;
  line-height: 28px;
  color: #666;
  width: 800px;
  height: 500px;
}

.work-score-title {
  margin-top: 50px;
  font-size: 24px;
  font-weight: bold;
  color: #444;
}

.score-title {
  margin-top: 20px;
  display: flex;
  align-items: baseline;
  font-size: 60px;
  font-weight: bold;
  color: #fdcb9e;
  float: right;
  margin-right: 20px;
}

.score-number {
  font-size: 33px;
  margin-right: 10px;
}

.score-total {
  font-size: 30px;
}

.score-comment-title {
  margin-top: 20px;
  font-size: 20px;
  font-weight: bold;
  color: #444;
}

.score-comment-content {
  margin-top: 20px;
  font-size: 16px;
  line-height: 28px;
  color: #666;
}

.operate-buttons {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

.score-dialog {
  padding: 20px;
}
</style>